package tech.aistar.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.constants.MessageConstant;
import tech.aistar.mapper.FlowerMapper;
import tech.aistar.mapper.OrderMapper;
import tech.aistar.pojo.Flower;
import tech.aistar.pojo.Order;
import tech.aistar.pojo.result.ResultReturn;
import tech.aistar.service.OrderService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private FlowerMapper flowerMapper;
    @Override
    public ResultReturn pageQuery(Integer userId, Integer pageNum, Integer pageSize) {
        List<Order> list = orderMapper.getOrderStatusListByUserId(userId);
        List<String> orderIds = orderMapper.getOrderIdsByUserId(userId);
        List<String> sendTimes = orderMapper.getSendTimesByUserId(userId);

        for(int i = 0 ;i<list.size();i++){
            list.get(i).setUser_id(userId);
            list.get(i).setOrder_id(orderIds.get(i));
            list.get(i).setSend_time(sendTimes.get(i));
            List<Integer> flowerIds =  orderMapper.getFlowerIdsByOrderId(list.get(i).getOrder_id());
            List<Flower> flowers = new ArrayList<>();
            for(Integer flowerId : flowerIds){
                flowers.add(flowerMapper.findById(flowerId));
            }
            list.get(i).setFlowers(flowers);
            List<Integer> counts = orderMapper.getCountsByOrderId(list.get(i).getOrder_id());
            list.get(i).setCounts(counts);
        }



        list = list.stream().sorted().toList();

        int count = list.size();
        int i = (pageNum - 1) * pageSize;
        int end = pageNum * pageSize;
        List<Order> data = new java.util.ArrayList<>(List.of());
        while(i<end && i<count){
            data.add(list.get(i));
            i++;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("list",data);
        map.put("total",count);
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        return ResultReturn.success(map);
    }

    @Override
    public ResultReturn initOrder(Order order) {
        Integer i = orderMapper.initOrder(order);
        if (i >= 0) {
            return ResultReturn.success();
        }
        return ResultReturn.fail(MessageConstant.FAIL);
    }

    @Override
    public ResultReturn delOrder(String orderId) {
        Integer i = orderMapper.delOrder(orderId);
        if (i >= 0) {
            return ResultReturn.success();
        }
        return ResultReturn.fail(MessageConstant.FAIL);
    }

    @Override
    @Transactional
    public ResultReturn updateOrder(Order order) {
        try {
            orderMapper.updateOrderAddress(order.getOrder_id(),order.getUser_id(),order.getAddress());
            orderMapper.updateOrderNote(order.getOrder_id(),order.getUser_id(),order.getNote());
            orderMapper.updateOrderSendTime(order.getOrder_id(),order.getUser_id(),order.getSend_time());
        }catch (Exception e){
            return ResultReturn.fail(MessageConstant.FAIL);
        }
        return ResultReturn.success();
    }
}
